package cn.zn.wx.config;

import cn.zn.framework.common.vo.FinanceValues;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WxMpConfig {
    @Autowired
    private FinanceValues financeValues;
//    @Autowired
//    private EventWinMessageHandler eventWinMessageHandler;
//    @Autowired
//    private DefaultMessageHandler defaultMessageHandler;

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
        wxMpDefaultConfig.setAppId(financeValues.getWxMp().getAppId());
        wxMpDefaultConfig.setSecret(financeValues.getWxMp().getAppSecret());
        wxMpDefaultConfig.setToken(financeValues.getWxMp().getToken());
        wxMpDefaultConfig.setAesKey(financeValues.getWxMp().getAesKey());
        return wxMpDefaultConfig;
    }

    @Bean
    public WxMpService wxMpService(WxMpConfigStorage wxMpConfigStorage) {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
        return wxMpService;
    }

    @Bean
    public WxMpMessageRouter wxMpMessageRouter(WxMpService wxMpService) {
        WxMpMessageRouter router = new WxMpMessageRouter(wxMpService);
//        router
//                // 4个条件必须全部匹配的路由规则
//                .rule()
//                .async(false)
//                .msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.SCANCODE_WAITMSG)
//                .eventKey("win")
//                .handler(eventWinMessageHandler)
//                .end()
//                // 兜底路由规则，一般放到最后
//                .rule()
//                .async(false)
//                .handler(defaultMessageHandler)
//                .end();
        return router;
    }
}
